/**
 * output package name
 */
package com.kingdee.eas.dahong.client;

import java.awt.event.ActionEvent;
import java.util.HashSet;
import java.util.Set;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.kdf.table.KDTSelectManager;
import com.kingdee.bos.ctrl.kdf.table.event.KDTMouseEvent;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.basedata.master.material.MaterialCollection;
import com.kingdee.eas.basedata.master.material.MaterialFactory;
import com.kingdee.eas.util.client.KDTableUtil;
import com.kingdee.util.StringUtils;

/**
 * output class name
 */
public class MaterialSelUI extends AbstractMaterialSelUI {

	private MaterialSelSelector sel = null;
	private String number = null;

	public MaterialSelUI(String number) throws Exception {
		super();
		this.number = number;
		// this.cell = cellParam;
	}

	public MaterialSelUI() throws Exception {
		super();
		kDTable1.getSelectManager().setSelectMode(KDTSelectManager.MULTIPLE_ROW_SELECT);
	}

	public void onLoad() throws Exception {
		kDTable1.getSelectManager().setSelectMode(KDTSelectManager.MULTIPLE_ROW_SELECT);
		number = (String) getUIContext().get("number");
		sel = (MaterialSelSelector) getUIContext().get("mySel");
		super.onLoad();
		fillTable();
	}

	private void fillTable() {
		if (StringUtils.isEmpty(number)) {
			this.queryF7MaterialImSalesQuery = new EntityViewInfo();
		} else {
			FilterInfo filter = new FilterInfo();
			filter.getFilterItems().add(new FilterItemInfo("model", "%" + number + "%", CompareType.LIKE));
			filter.getFilterItems().add(new FilterItemInfo("helpCode", "%" + number + "%", CompareType.LIKE));
			filter.getFilterItems().add(new FilterItemInfo("materialGroup.name", "%" + number + "%", CompareType.LIKE));
			filter.setMaskString("#0 or #1 or #2");
			queryF7MaterialImSalesQuery = new EntityViewInfo();
			queryF7MaterialImSalesQuery.setFilter(filter);
		}
		super.kDTable1.removeRows();
		kDTable1.setEnabled(false);
		if (kDTable1.getRowCount() > 0) {
			kDTable1.getSelectManager().select(0, 0);
		}

	}

	protected void btnQuery_actionPerformed(ActionEvent e) throws Exception {
		String numberVal = txtNumber.getText();
		String modelVal = txtModel.getText();

		if (StringUtils.isEmpty(numberVal) && StringUtils.isEmpty(modelVal)) {
			this.queryF7MaterialImSalesQuery = new EntityViewInfo();
		} else {
			FilterInfo filterMain = new FilterInfo();

			// filter.getFilterItems().add(new FilterItemInfo("model",
			// number,CompareType.LIKE));
			// filter.getFilterItems().add(new FilterItemInfo("helpCode",
			// number,CompareType.LIKE));
			// filter.getFilterItems().add(new
			// FilterItemInfo("materialGroup.name", number,CompareType.LIKE));
			// filter.setMaskString("#0 or #1 or #2");
			int fcount = 0;

			if (!StringUtils.isEmpty(numberVal)) {
				filterMain.getFilterItems().add(new FilterItemInfo("number", "%" + numberVal + "%", CompareType.LIKE));
				fcount++;
			}
			if (!StringUtils.isEmpty(modelVal)) {
				filterMain.getFilterItems().add(new FilterItemInfo("model", "%" + modelVal + "%", CompareType.LIKE));
				fcount++;
			}

			if (fcount > 0) {
				String mask = "";
				for (int i = 0; i < fcount; i++) {
					mask = mask + "#" + i;

					if (i != fcount - 1) {
						mask = mask + " or ";
					}
				}
				filterMain.setMaskString(mask);
			}
			queryF7MaterialImSalesQuery = new EntityViewInfo();
			queryF7MaterialImSalesQuery.setFilter(filterMain);
		}

		super.kDTable1.removeRows();
		kDTable1.setEnabled(false);
		if (kDTable1.getRowCount() > 0) {
			kDTable1.getSelectManager().select(0, 0);
		}

	}

	protected void kDTable1_tableClicked(KDTMouseEvent e) throws Exception {
		if (e.getClickCount() == 2) {
			btnConfirm_actionPerformed(null);
		}
	}

	@Override
	protected void btnConfirm_actionPerformed(ActionEvent e) throws Exception {
		sel.setCancel(false);
		buildRetData();
		// TODO Auto-generated method stub
		super.disposeUIWindow();
	}

	private void buildRetData() throws Exception {
		int[] selectedRows = KDTableUtil.getSelectedRows(kDTable1);
		Set ids = new HashSet();
		for (int i = 0; i < selectedRows.length; i++) {
			ids.add(kDTable1.getRow(selectedRows[i]).getCell("id").getValue().toString());
		}

		if (ids.size() != 0) {
			EntityViewInfo evi = new EntityViewInfo();
			FilterInfo ff = new FilterInfo();
			ff.getFilterItems().add(new FilterItemInfo("id", ids, CompareType.INCLUDE));
			evi.setFilter(ff);

			MaterialCollection materialCollection = MaterialFactory.getRemoteInstance().getMaterialCollection(evi);
			Object[] allData = materialCollection.toArray();
			sel.setData(allData);

		} else {
			sel.setData(null);
		}
	}

	protected void btnCancel_actionPerformed(ActionEvent e) throws Exception {
		// super.btnCancel_actionPerformed(e);
		sel.setCancel(true);
		sel.setData(null);
		// if (cell != null) {
		// cell.setValue(null);
		// }
		// getUIContext().clear();
		// this.destroyWindow();
		super.disposeUIWindow();
	}

}